package com.getguang.superstore.dialog

import android.view.View
import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentManager
import com.blankj.utilcode.util.ClipboardUtils
import com.getguang.superstore.databinding.DialogCouponCopyBinding
import com.starblink.android.basic.aroute.CommonRoute
import com.starblink.android.basic.ext.toastSuccess
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.sensorsdata.extension.findActivitySpmId
import com.starblink.android.basic.sensorsdata.extension.findActivitySpmParams
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.widget.dialog.BaseBottomSheetFragment
import com.starblink.rocketreserver.fragment.MerchantWebVOF

/**
 *  Create by yuanl at 2023/10/11
 **/
class CouponCodeCopyDF : BaseBottomSheetFragment() {

    private val binding by lazyOnNone {
        DialogCouponCopyBinding.inflate(layoutInflater)
    }

    var merchantDiscountInfo: MerchantWebVOF.MerchantDiscountInfo? = null
    override fun getBindingRootView(): View = binding.root

    override fun initView() {
        val code = arguments?.getString("code") ?: ""
        val des = arguments?.getString("des") ?: ""
        val url = arguments?.getString("url") ?: ""
        val merchantWebNo = arguments?.getString("merchantWebNo") ?: ""
        val index = arguments?.getInt("index")
        val merchantName = arguments?.getString("merchantName").orEmpty()

        binding.tvCode.text = code
        binding.tvDes.text = des

        binding.btnClose.setOnClickListener {
            dismiss()
        }
        binding.btn.setOnClickListener {
            ClipboardUtils.copyText(code)
            toastSuccess("Copied Successfully")
            CommonRoute.startStoreLinkWithCode(
                merchantWebNo,
                url,
                code,
                merchantDiscountInfo
            )
        }
        binding.btn.trackData(
            requireActivity().findActivitySpmId(),
            SpmElementDef.ELE_33092,
            params = mutableMapOf<String, String>().apply {
                requireActivity().findActivitySpmParams()?.takeIf { it.isNotEmpty() }?.let {
                    putAll(it)
                }
                put("index", index.toString())
                put("merchantName", merchantName)
            }
        )
    }

    companion object {

        @JvmStatic
        fun show(
            fragmentManager: FragmentManager,
            code: String,
            des: String,
            url: String?,
            index: Int,
            merchantName: String?,
            merchantWebNo: String?,
            merchantDiscountInfo: MerchantWebVOF.MerchantDiscountInfo?
        ) {
            CouponCodeCopyDF().apply {
                arguments = bundleOf(
                    "merchantWebNo" to merchantWebNo,
                    "code" to code,
                    "des" to des,
                    "url" to url,
                    "index" to index,
                    "merchantName" to merchantName,
                )
                this.merchantDiscountInfo = merchantDiscountInfo
            }.show(fragmentManager, null)
        }

    }

}